package com.acmen.easyapi.repository;

import com.acmen.easyapi.dbcp.JdbcService;
import com.acmen.easyapi.factory.EnvFactory;
import com.acmen.easyapi.model.App;
import com.acmen.easyapi.model.Environment;
import com.alibaba.fastjson.JSON;
import com.acmen.easyapi.util.StringUtil;
import com.acmen.easyapi.util.UUID;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;

;

/**
 * @author acmen
 */
@Component
public class EnvironmentRepository {
    private final static String QUERY_BY_ID_SQL = "select * from t_environment where id=?";

    private final static String QUERY_ALL_BY_APP_SQL = "select * from t_environment where app_name=? and " +
            "update_dt>date_add(now(),interval -1 day) order by idx";
    private final static String INSERT_SQL = "insert into t_environment(id,name,base_path,app_name,owner,idx," +
            "update_dt) values(?,?,?,?,?,?,?)";
    private final static String CHECK_SQL = "select id from t_environment where app_name=? and name=? and base_path=?";
    private final static String DELETE_SQL = "delete from t_environment where app_name=? and name=? and base_path=?";
    private final static String UPDATE_SQL = "update t_environment set update_dt=? where id=?";
    @Resource
    private JdbcService jdbcService;

    @Resource
    private AppRepository appRepository;

    @Resource
    private EnvFactory envFactory;

    /**
     * 根据ID查询出记录
     *
     * @return
     */
    public List<Environment> getGatewayEnvironments(String appName) {
        App app = appRepository.selectByName(appName);
        String route = app.getRoute();
        if(StringUtil.isEmpty(route)){
            route = "/未配置路由";
        }else if("/".equals(route.trim())){
            route = "";
        }
        List<Environment> environments = JSON.parseArray(JSON.toJSONString(envFactory.getEnvironmens()),Environment.class);
        for(Environment env : environments){
            env.setBasePath(env.getBasePath()+route);
            env.setOwner("Gateway");
        }
        return environments;
    }

    public Environment getById(String id) {
        return jdbcService.selectOne(QUERY_BY_ID_SQL, new Object[]{id},Environment.class);
    }

    public List<Environment> getAllByApp(String appName) {
        List<Environment> environments = jdbcService.selectList(QUERY_ALL_BY_APP_SQL, new Object[]{appName},
                Environment.class);
        environments.addAll(0,getGatewayEnvironments(appName));
        return environments;
    }


    public boolean create(String appName, Environment environment) {
        if (StringUtil.isEmpty(environment.getName())) {
            return false;
        }
        Map check = jdbcService.selectOne(CHECK_SQL,new Object[]{appName, environment.getName(), environment.getBasePath()});
        if(check == null){
            Object[] args = {UUID.genUUID32(), environment.getName(),
                    environment.getBasePath(), appName, environment.getOwner(), 1, new Date()};
            return jdbcService.update(INSERT_SQL, args) > 0;
        }else{
            return jdbcService.update(UPDATE_SQL,new Object[]{new Date(),check.get("id")})>0;
        }
    }

    public boolean exits(String appName, String name, String basePath) {
        Map dataMap = jdbcService.selectOne(CHECK_SQL, new Object[]{appName, name, basePath});
        return dataMap != null;
    }
}
